package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsAdminBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/comment")
public class DtsAdminCommentController {

    @Autowired
    DtsCommentService commentService;
    @Autowired
    DtsAdminBrandService adminBrandService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment dtsComment){
        Subject subject = SecurityUtils.getSubject();
        if(!subject.hasRole("超级管理员")){
            return ResponseUtil.fail(-1,"没有权限删除,请联系管理员");
        }
        commentService.delete(dtsComment);
        return ResponseUtil.ok();
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                       String sort, String order, Integer userId, Integer valueId){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> list = null;
        if (subject.hasRole("品牌制造商")) {
            list = adminBrandService.selectId(admin.getId());
        }
        IPage<DtsComment> iPage = commentService.queryPage(page,limit,sort,order,userId,valueId,list);

        Map<String,Object> data= new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
